package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.ClientDao;
import lxt.entity.ClientInfo;
/**
 * 客户管理dao
 * @author Lit
 *
 */
public class ClientDaoImpl extends ABseDaoImpl implements ClientDao {
	/**
	 * 根据具体信息查询
	 */
	@SuppressWarnings("unchecked")
	public List<ClientInfo> loadAllClient(String linkman,String address,String clientName,int offset, int length){
		//查询的hql
		StringBuffer hql = new StringBuffer("select c from ClientInfo c where 1=1 ");
		//存入字段
		ArrayList<Object> param = new ArrayList<Object>();
		
		if(linkman!=null && !linkman.equals("")){
			hql.append(" And c.linkman = ?");
			param.add(linkman);
		}
		if(address!=null && !address.equals("")){
			hql.append(" And c.address like ?");
			param.add("%"+address+"%");
		}
		if(clientName!=null && !clientName.equals("")){
			hql.append(" And c.clientName = ?");
			param.add(clientName);
		}
		
		List<ClientInfo> clients = (List<ClientInfo>) this.queryForPage(hql.toString(),param, offset, length);
		
		return clients;
	}
	/**
	 * 查询数量
	 */
	public int loadAllClientCount(String linkman,String address,String clientName){
		//查询的hql
		StringBuffer hql = new StringBuffer("select c from ClientInfo c where 1=1 ");
		//存入字段
		ArrayList<Object> param = new ArrayList<Object>();
		
		if(linkman!=null && !linkman.equals("")){
			hql.append(" And c.linkman = ?");
			param.add(linkman);
		}
		if(address!=null && !address.equals("")){
			hql.append(" And c.address like ?");
			param.add("%"+address+"%");
		}
		if(clientName!=null && !clientName.equals("")){
			hql.append(" And c.clientName = ?");
			param.add(clientName);
		}
		
		return this.getAllRowCount(hql.toString(), param);
	}
	/**
	 * 查询订单数by客户
	 */
	public int loadCountOrdersForClient(String clientId) {
		//查询的hql
		StringBuffer hql = new StringBuffer("select i from InvoiceOrder i where i.clientInfoByRClient.clientId = ? or i.clientInfoBySClient.clientId = ?");
		//存入字段
		ArrayList<Object> param = new ArrayList<Object>();
		param.add(clientId);
		param.add(clientId);
		return this.getAllRowCount(hql.toString(), param);
	}
}
